/*
 * Package il.ac.biu.cs.grossmm.test.fooAdapters
 * File FooFormatAdapter.java
 * 
 * This is a part of presence service framework API. 
 * See javadoc for more information.
 *  
 * Copyright (C) 2006 Grossmann Mark
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package il.ac.biu.cs.grossmm.tests.fooAdapters;

import static il.ac.biu.cs.grossmm.api.data.NodeOf.*;
import static il.ac.biu.cs.grossmm.api.data.NodeTypeByInterface.*;
import static il.ac.biu.cs.grossmm.api.keys.KeyPattern.*;
import static il.ac.biu.cs.grossmm.api.keys.PatternFactory.*;
import static il.ac.biu.cs.grossmm.api.presence.BaseVocabulary.*;
import il.ac.biu.cs.grossmm.api.OperationFailedException;
import il.ac.biu.cs.grossmm.api.data.Node;
import il.ac.biu.cs.grossmm.api.data.NodeOf;
import il.ac.biu.cs.grossmm.api.data.Root;
import il.ac.biu.cs.grossmm.api.flow.ConditionalConverterBase;
import il.ac.biu.cs.grossmm.api.flow.ConverterProxyBase;
import il.ac.biu.cs.grossmm.api.flow.ConverterProxy;
import il.ac.biu.cs.grossmm.api.flow.NodeInspector;
import il.ac.biu.cs.grossmm.api.flow.NodeManipulator;
import il.ac.biu.cs.grossmm.foo.format.Presence;
import il.ac.biu.cs.grossmm.foo.format.PresenceDoc;
import il.ac.biu.cs.grossmm.foo.format.PresenceTuple;
import il.ac.biu.cs.grossmm.foo.format.PresenceDoc.Tuple;

import java.util.Set;

public class FooFormatAdapter extends
	ConditionalConverterBase<Presence, NodeOf<String>> {

	public FooFormatAdapter()
	{
		super("FooFormatAdapter", nodeType(Presence.class), ANY_KEY,
			nodeOf(String.class), maskPattern(Object.class, mandatory(
				CONTENT_TYPE, "foo.PresenceDoc")), 1);
	}

	class Proxy extends ConverterProxyBase<Presence, NodeOf<String>> {

		public Proxy(Root<Presence> srcRoot, Root<NodeOf<String>> dstRoot,
			NodeInspector srcInspector, NodeManipulator dstManipulator)
			throws OperationFailedException
		{
			super(srcRoot, dstRoot, srcInspector, dstManipulator);
		}

		@Override
		protected void convert(NodeInspector ins, NodeManipulator man)
			throws OperationFailedException
		{
			PresenceDoc doc = new PresenceDoc();

			doc.setEntity(ins.getValue(srcRoot, Presence.ENTITY));
			doc.setActivities(ins.getValue(srcRoot, Presence.ACTIVITIES));
			doc.setLocation(ins.getValue(srcRoot, Presence.LOCATION));
			doc.setMood(ins.getValue(srcRoot, Presence.MOOD));
			doc.setSphere(ins.getValue(srcRoot, Presence.SPHERE));

			Set<Node<PresenceTuple>> set =
				ins.getSubnodes(srcRoot, Presence.TUPLES);

			Tuple[] tuples = new Tuple[set.size()];

			int i = 0;
			for (Node<PresenceTuple> node : set) {
				Tuple t = new Tuple();

				t.setId(ins.getValue(node, PresenceTuple.ID));
				t.setPriority(ins.getValue(node, PresenceTuple.PRIORITY));
				t.setAddress(ins.getValue(node, PresenceTuple.ADDRESS));
				t.setCapabilities(ins.getValue(node, PresenceTuple.CAPABILITIES));
				t.setStatus(ins.getValue(node, PresenceTuple.STATUS));
				t.setTupleClass(ins.getValue(node, PresenceTuple.CLASS));

				tuples[i++] = t;
			}

			doc.setTuples(tuples);

			man.setValue(dstRoot, property(String.class), doc.serialize());
		}
	}

	public ConverterProxy createProxy(Root srcNode, Root dstNode,
		NodeInspector srcInspector, NodeManipulator dstManipulator)
		throws OperationFailedException
	{
		return new Proxy(srcNode, dstNode, srcInspector, dstManipulator);

	}

}
